from fastapi import FastAPI, APIRouter
from fastmcp import FastMCP
import uvicorn

# 创建FastMCP实例
mcp = FastMCP("my-mcp-server")


# 定义MCP工具
@mcp.tool()
def get_user_info(user_id: str) -> dict:
    """获取用户信息"""
    return {"user_id": user_id, "name": f"User-{user_id}", "status": "active"}

@mcp.tool()
def calculate_sum(a: int, b: int) -> int:
    """计算两个数的和"""
    return a + b

# 创建FastAPI路由
router = APIRouter()

@router.get("/get_count")
async def get_count():
    """获取计数信息"""
    return {"count": 42, "message": "Success"}

@router.get("/health")
async def health_check():
    """健康检查端点"""
    return {"status": "healthy", "service": "fastapi"}

# 创建主FastAPI应用
app = FastAPI(
    title="Hybrid MCP & FastAPI Server",
    description="同时支持MCP工具和传统API的混合服务器",
    version="1.0.0",
    # lifespan=mcp.http_app().lifespan  # 关键：传递MCP的生命周期上下文
)
# 挂载MCP服务到 /mcp 路径
app.mount("/mcp", mcp.http_app())

# 包含普通API路由
app.include_router(router, prefix="/api")

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)